﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace Wild2.Controllers
{
    public class OfficeController : Controller
    {
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index()
        {
            Wild2.Models.Pages.Office.Index page = new Models.Pages.Office.Index();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (UserID.HasValue)
            {
                page.Aphorism = Wild2.Models.Aphorisms.Aphorisms.GetAphorism();
                page.UserData = Wild2.Models.User.User.GetUserData(UserID.Value);
                page.isAvatar = System.IO.File.Exists(Request.MapPath("~/Content/avatars/" + UserID.Value + ".png"));
            }
            else
            {
                return RedirectToAction("Index", "Login");
            }
            return View(page);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(String homeurl)
        {
            Wild2.Models.Pages.Office.Index page = new Models.Pages.Office.Index();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (UserID.HasValue)
            {
                if (homeurl != null) Wild2.Models.User.Settings.SetUserHomeUrl(UserID.Value, homeurl);
                page.UserData = Wild2.Models.User.User.GetUserData(UserID.Value);
                page.Aphorism = Wild2.Models.Aphorisms.Aphorisms.GetAphorism();
                page.isAvatar = System.IO.File.Exists(Request.MapPath("~/Content/avatars/" + UserID.Value + ".png"));
            }
            else
            {
                return RedirectToAction("Index", "Register");
            }
            return View(page);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Address(Int32? id)
        {
            Wild2.Models.Pages.Office.Address page = new Models.Pages.Office.Address();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (UserID.HasValue)
            {
                page.Cities = new Dictionary<int, string>();
                if (id.HasValue)
                {
                    String address = "Не указан";
                    if (id.Value > 0)
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.Load(Request.MapPath("~/Content/cities.xml"));
                        XmlNodeList list = doc.SelectNodes("/cities/country/city[@id='" + id.Value + "']");
                        address = list.Item(0).Attributes["country"].Value;
                        if (!String.IsNullOrEmpty(list.Item(0).Attributes["part"].Value)) address += ", " + list.Item(0).Attributes["part"].Value;
                        address += ", " + list.Item(0).InnerText.Trim();
                        
                    }
                    Wild2.Models.User.User.SetUserAddress(UserID.Value, address);
                    return RedirectToAction("Index", "Office");
                }
            }
            else
            {
                return RedirectToAction("Index", "Login");
            }
            return View(page);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Address(String city)
        {
            Wild2.Models.Pages.Office.Address page = new Models.Pages.Office.Address();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (UserID.HasValue)
            {
                page.Cities = new Dictionary<int, string>();
                if (city.Length > 2)
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(Request.MapPath("~/Content/cities.xml"));
                    XmlNodeList list = doc.SelectNodes("/cities/country/city[contains(., '" + city[0].ToString().ToUpper() + city.Substring(1) + "')]");
                    for (int i = 0; i < list.Count; i++)
                    {
                        String address = list.Item(i).Attributes["country"].Value;
                        if (!String.IsNullOrEmpty(list.Item(i).Attributes["part"].Value)) address += ", " + list.Item(i).Attributes["part"].Value;
                        address += ", " + list.Item(i).InnerText.Trim();
                        page.Cities.Add(Convert.ToInt32(list.Item(i).Attributes["id"].Value), address);
                    }
                }
                else
                {
                    page.infoMessage = "Введите первые 3 буквы названия вашего города";
                }
            }
            else
            {
                return RedirectToAction("Index", "Login");
            }
            return View(page);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Profile()
        {
            Wild2.Models.Pages.Office.Profile page = new Models.Pages.Office.Profile();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (UserID.HasValue)
            {
                Wild2.Models.Pages.User.Profile profile = Wild2.Models.User.User.GetProfileData(UserID.Value);
                page.birth = profile.birth;
                page.email = profile.email;
                page.isMale = profile.isMale;
                page.key = profile.key;
                page.password = profile.password;
            }
            else
            {
                return RedirectToAction("Index", "Login");
            }
            return View(page);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Profile(String day, String month, String year, String email, String male)
        {
            Wild2.Models.Pages.Office.Profile page = new Models.Pages.Office.Profile();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (UserID.HasValue)
            {
                Wild2.Models.Pages.User.Profile profile = new Wild2.Models.Pages.User.Profile();

                int birthDay = 0;
                int.TryParse(day, out birthDay);
                int birthMonth = 0;
                int.TryParse(month, out birthMonth);
                int birthYear = 0;
                int.TryParse(year, out birthYear);
                if (birthDay > 0 && birthDay <= 31 && birthMonth > 0 && birthMonth <= 12 && birthYear >= 1950 && birthYear <= 2005)
                {
                    try
                    {
                        profile.birth = new DateTime(birthYear, birthMonth, birthDay);
                    }
                    catch (Exception)
                    {

                    }
                }
                if (male == "male") profile.isMale = true;
                if (male == "female") profile.isMale = false;

                profile.id = UserID.Value;
                profile.email = email;
                
                Wild2.Models.User.User.SetProfileData(profile);
                return RedirectToAction("Index", "Office");
            }
            else
            {
                return RedirectToAction("Index", "Login");
            }
        }

        public ActionResult Invites()
        {
            Wild2.Models.Pages.Office.Invites page = new Models.Pages.Office.Invites();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (UserID.HasValue)
            {
                page.invites = Wild2.Models.User.Invite.GetInvites(UserID.Value);
            }
            else
            {
                return RedirectToAction("Index", "Login");
            }
            return View(page);
        }

        public ActionResult Subscriptions()
        {
            Wild2.Models.Pages.Office.Subscriptions page = new Models.Pages.Office.Subscriptions();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (UserID.HasValue)
            {
                page.subscriptions = Wild2.Models.User.Subscription.GetSubscriptions(UserID.Value);
            }
            else
            {
                return RedirectToAction("Index", "Login");
            }
            return View(page);
        }
    }
}
